package com.share.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * @desc:
 * @author:caifan
 * @date:2022/4/1
 */
public class SemaphoreDemo01 {
    private static final ExecutorService pool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for (int i = 0; i < 100; i++) {
            pool.submit(new Task(semaphore));
        }
        pool.shutdown();
    }

    private static class Task implements Runnable {
        private Semaphore semaphore;

        public Task(Semaphore semaphore) {
            this.semaphore = semaphore;
        }
        @Override
        public void run() {

            try {
                semaphore.acquire();
                System.out.println(Thread.currentThread() + ":慢服务");
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            semaphore.release();
            System.out.println(Thread.currentThread() + "释放许可");
        }
    }
}
